m = int(input())
if len(str(m)) != 4 or m <= 0:
    print("0")
else:
    t = m // 1000 #1
    sot = m // 100 % 10 #7
    des = m // 10 % 10 #3
    ed = m % 10 #7
    x = 0
    b = [t, sot, des, ed, t * 10 + sot, t * 100 + sot*10 + des, m, sot*10 + des, sot * 100 + des*10 + ed, des * 10 + ed]
    b.sort()
    b.reverse()
    for i in range(len(b)):
        c = 0
        for x in range(2, b[i]//2):
            if b[i] % x == 0:
                c += 1
        if c == 0:
            x = b[i]
            break

    print(x)




